<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends Model {
	
	function Admin()
	{
		parent::Model();
                // Other stuff
		$this->_prefix = $this->config->item('DX_table_prefix');
		$this->_table = $this->_prefix.$this->config->item('DX_users_table');
		$this->_roles_table = $this->_prefix.$this->config->item('DX_roles_table');
	}
	
	function index()
	{
		
	}

        // User table function

	function create_user($data)
	{
		$data['created'] = date('Y-m-d H:i:s', time());
		return $this->db->insert($this->_table, $data);
	}

	function get_user_field($user_id, $fields)
	{
		$this->db->select($fields);
		$this->db->where('id', $user_id);
		return $this->db->get($this->_table);
	}

	function set_user($user_id, $data)
	{
		$this->db->where('id', $user_id);
		return $this->db->update($this->_table, $data);
	}

	function delete_user($user_id)
	{
		$this->db->where('id', $user_id);
		$this->db->delete($this->_table);
		return $this->db->affected_rows() > 0;
	}

        function parse_csv($filename,$classname,$teacherid)
        {
                $this->load->database();
                $data1 = array(
                   'name' => $classname,
                   'teacher' =>$teacherid
                   );
                $this->db->insert('class', $data1);
                $classid = $this->db->insert_id();//查询上次插入后的ID

                //设置默认时区为中国时区
                date_default_timezone_set('PRC');
                $creatdate=date ("Y-m-d H:i:s");
                $handle = fopen ($filename,"r");
                $data = fgetcsv ($handle);
                while ($data = fgetcsv ($handle)) {
                   $data1 = array(
                   'role_id' => '1' ,
                   'username' =>$data[2] ,
                   'password' =>'$1$4F/.5M0.$jGTlZ3M675BhGlrFtuCZ30' ,//默认密码123456
                   'email' =>$data[1] ,
                   'created' => $creatdate
                   );
                   $this->db->insert('users', $data1);
                   //$sql1='insert into users(role_id,username,password,email,created) values("1","'.$data[2].'","$1$4F/.5M0.$jGTlZ3M675BhGlrFtuCZ30","'.$data[1].'",NOW())';
                   //echo $sql1;
                   //$excu=mysql_query($sql1,$id);
                   //$sql="select last_insert_id() ";
                   //$excu=mysql_query($sql,$id);
                   $lastuserid = $this->db->insert_id();//查询上次插入后的ID
                   //$lastid=mysql_result($excu,0);
                   $data1 = array(
                   'user_id' => $lastuserid ,
                   'number' =>$data[0] ,
                   'sex' =>$data[4] ,
                   'age' =>$data[3] ,
                   'office' => $data[5]
                   );
                   $this->db->insert('user_profile', $data1);
                   //$sql2='insert into user_profile(user_id,number,sex,age,office) values("'.$lastid.'","'.$data[0].'","'.$data[4].'","'.$data[3].'","'.$data[5].'")';
                   //echo $sql2;
                   //$excu=mysql_query($sql2,$id);
                   $data1 = array(
                   'userid' => $lastuserid,
                   'classid' =>$classid
                   );
                   $this->db->insert('userstoclass', $data1);
                }
                //mysql_close($id);
                fclose ($handle);
                if(is_file($filename)) //判断是否存在该文件
                {
                      @unlink($filename); //删除文件
                }

                $data['info']="学生信息已添加，默认密码为123456";//"上传成功";
                $data['url']=site_url('admin/manage/addclass');
                $this->load->view('admin/info',$data);
        }

	
}
?>